package xyz.yuzegod.megawalls.classes.normal.spider;

import net.citizensnpcs.api.CitizensAPI;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.nms.CustomSpider;
import xyz.yuzegod.megawalls.util.EntityTypes;
import xyz.yuzegod.megawalls.util.ItemBuilder;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Spider extends Classes
{
    public static Map<GamePlayer, Integer> skill2;
    
    public Spider() {
        super("Spider", "蜘蛛", ChatColor.DARK_PURPLE, Material.WEB, (byte)0, ClassesType.NORMAL, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.AGILITY, ClassesInfo.Orientation.CHARGER }, ClassesInfo.Difficulty.THREE);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDk0Mzc4MjcwMzgsInByb2ZpbGVJZCI6IjkxZjA0ZmU5MGYzNjQzYjU4ZjIwZTMzNzVmODZkMzllIiwicHJvZmlsZU5hbWUiOiJTdG9ybVN0b3JteSIsInNpZ25hdHVyZVJlcXVpcmVkIjp0cnVlLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTMwOTliZWRlZTE3Y2JhMzA3ODU3MmQzMjM3NWE1YTRmYjgwMjZiMzA1ZmViM2IwZGExNTMzYjdkNmEyZDlhYyJ9fX0=", "j2Cu6xMoeD4a0BURXPtMu801DioMRj/Onw2vr9+fpT8CX9w7ztz7zGGl2Pb4jWTWuqW7GEvbivWA6G4UWMpxdLHnmmOqkRa2U3N3uAw+YUYAji2Q086HIkaHZTVCmDkxskrySnUuOaN71ck+TD+qS6cJS3AeAvMd/cwKyEricS5GbTssbms5X7hL58s7j9Rb42i3MUl3B0oo++l2vxDe0q3f0NB1K6YlpnnHNgvZKRww9KPk2iUbqrDNZFHX7xIfndAYrvQvUATyQqAAeaDdi2VxoocMOKdfNvTey6g4OD4YyrK62shYV8xD8p+DVU1NjtPSJlyBu9s9vxej9mFxGpct8VndHvntzWID64cWT/dzK9PyMUmRhoPQejVZ8n1+Xl/QzHIyPvT8mjHMmdW+eHmh/eqXCLK4OkRWL8CO9yUMgDktVC/reFR8rveiqoPMkdeV6CN3gGOfgmPdtWbemoytNY0q7MbWfrBuSqpv6q1lL1eA4I/ODH5DJE+EFrfA4QRLakArKtD+sw+gWtp2IwQqtN6X6rGLAbEw6M84wrZ0gNmB1Kz9OLO/Lw193nvTXaHWpiO0/NXdES8OJX96wGlQcpaRUo5v0QnsbEvHbG7SJkDP8gN5aX2FnO+S96XBuFm02cCj7TXF8NRH2WfHj3c99wXNX5kFZ+8l+IxW3Ys=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_SPADE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 锹").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.DIG_SPEED, 1).build());
                        items.add(new ItemBuilder(Material.IRON_CHESTPLATE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_SPADE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 锹").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.DIG_SPEED, 1).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_SPADE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 锹").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.DIG_SPEED, 1).build());
                        items.add(new ItemBuilder(Material.IRON_BOOTS).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_SPADE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 锹").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.DIG_SPEED, 1).build());
                        items.add(new ItemBuilder(Material.DIAMOND_BOOTS).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.DAMAGE_ARTHROPODS, 4).build());
                        items.add(new ItemBuilder(Material.IRON_SPADE).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 锹").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.DIG_SPEED, 1).build());
                        items.add(new ItemBuilder(Material.DIAMOND_BOOTS).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 靴子").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Spider.this.nameColor + Spider.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7治疗药水 8❤");
                        lore.add(" §8▪ §7铁 锹");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 效率 I");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 保护 I");
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add(" §8▪ §7速度药水 §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 节肢杀手 IV");
                        lore.add(" §8▪ §7钻石 靴子");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §a+ 保护 I");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你喜欢伏击落单的敌人.");
        lore.add("§7你喜欢在高空作战.");
        lore.add("§7你不怕蜘蛛.");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 25000;
    }
    
    @Override
    public int energyMelee() {
        return 8;
    }
    
    @Override
    public int energyBow() {
        return 8;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        final String second = this.secondSkill.getSkillTip(gamePlayer);
        return this.mainSkill.getSkillTip(gamePlayer) + ((second == null) ? "" : (" " + second));
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> gamePlayer.sendActionBar(this.getSkillTip(gamePlayer)));
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamage(final EntityDamageEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.isCancelled()) {
            return;
        }
        if (!(e.getEntity() instanceof Player) || e.getCause() != EntityDamageEvent.DamageCause.FALL) {
            return;
        }
        final GamePlayer gamePlayer = GamePlayer.get(e.getEntity().getUniqueId());
        final Classes classes = ClassesManager.getSelected(gamePlayer);
        if (classes.equals(this)) {
            if (Spider.skill2.getOrDefault(gamePlayer, 0) == 0) {
                return;
            }
            final Player player = (Player)e.getEntity();
            Spider.skill2.put(gamePlayer, Spider.skill2.get(gamePlayer) - 1);
            ParticleEffect.HEART.display(0.0f, 0.0f, 0.0f, 1.0f, 3, player.getLocation(), 10.0);
            player.getWorld().playSound(player.getLocation(), Sound.SPIDER_IDLE, 1.0f, 0.0f);
            PlayerUtils.heal(player, Spider.skill2.getOrDefault(gamePlayer, 0) * 1.5);
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity()) || !(e.getDamager() instanceof Player)) {
            return;
        }
        if (e.isCancelled()) {
            return;
        }
        final GamePlayer gamePlayer = GamePlayer.get(e.getDamager().getUniqueId());
        if (gamePlayer.isSpectator() || gamePlayer.getGameTeam().isInTeam(GamePlayer.get(e.getEntity().getUniqueId()))) {
            return;
        }
        final Classes classes = ClassesManager.getSelected(gamePlayer);
        if (classes.equals(this)) {
            Classes.startAddEnergyTimer(gamePlayer, 5, 3);
            final int times = Spider.skill2.getOrDefault(gamePlayer, 0);
            if (times == 0) {
                return;
            }
            final int hits = ((MainSkill)this.mainSkill).getHits(this.mainSkill.getPlayerLevel(gamePlayer));
            if (times == hits) {
                final Spider spider = this;
                Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
                    @Override
                    public void run() {
                        if (times == 0) {
                            Spider.this.thirdSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(spider));
                        }
                    }
                }, 60L);
            }
            Spider.skill2.put(gamePlayer, Spider.skill2.get(gamePlayer) - 1);
            ((Player)e.getEntity()).damage(e.getFinalDamage() * 0.25);
        }
    }
    
    @EventHandler
    public void onPlayerDeath(final PlayerDeathEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC((Entity)e.getEntity())) {
            return;
        }
        final GamePlayer gamePlayer = GamePlayer.get(e.getEntity().getUniqueId());
        final Classes classes = ClassesManager.getSelected(gamePlayer);
        if (classes.equals(this)) {
            final CustomSpider spider = new CustomSpider((World)((CraftWorld)Bukkit.getWorld("world")).getHandle());
            spider.setGamePlayer(gamePlayer);
            spider.getBukkitEntity().setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
            EntityTypes.spawnEntity((net.minecraft.server.v1_8_R3.Entity)spider, gamePlayer.getPlayer().getLocation());
        }
    }
    
    static {
        Spider.skill2 = new HashMap<GamePlayer, Integer>();
    }
}
